package com.qezhhnjy.flink.job;

import com.qezhhnjy.flink.mapper.MyFlatMapper;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author zhaoyangfu
 * @date 2021/8/24-16:08
 * --host 192.168.16.50 --port 9999
 */
public class StreamWordCount {

    public static void stream(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        ParameterTool parameterTool = ParameterTool.fromArgs(args);
        String host = parameterTool.get("host");
        int port = parameterTool.getInt("port");

        env.socketTextStream(host, port)
                .flatMap(new MyFlatMapper())
                .keyBy((KeySelector<Tuple2<String, Integer>, String>) value -> value.f0)
                .sum(1)
                .print()
                .setParallelism(2);

        env.execute();
    }
}
